package com.one.consumer.reserve.presenter;

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.bean.BannerBean;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.FieldCheckUtil;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.consumer.common.extra.LocationExtra;
import com.one.consumer.common.map.LocationUtils;
import com.one.consumer.reserve.model.ReserveModel;
import com.one.consumer.reserve.model.bean.DEStyleBean;
import com.one.common.common.order.extra.CustomExtra;
import com.one.common.common.order.extra.ReserveExtra;
import com.one.consumer.reserve.ui.view.AddReserveView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/22 下午9:11
 * description ：
 **/
public class AddReserveDePresenter extends BaseApiPresenter<AddReserveView, ReserveModel> {
    private ReserveExtra mExtra;

    public AddReserveDePresenter(AddReserveView mView, Context mContext) {
        super(mView, mContext, new ReserveModel((BaseActivity) mContext));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mExtra = (ReserveExtra) mActivity.getIntent().getSerializableExtra(ReserveExtra.getExtraName());
    }

    /**
     * 获取首页banner
     */
    public void getBanner() {
        mModel.getBanner(new ObserverOnResultListener<ArrayList<BannerBean>>() {
            @Override
            public void onResult(ArrayList<BannerBean> bannerBeans) {
                if (mView != null) {
                    mView.setBanner(bannerBeans);
                }
            }
        });
    }

    /**
     * 获取装修样式
     */
    public void getStyles() {
        mModel.getStyles(new ObserverOnResultListener<ArrayList<DEStyleBean>>() {
            @Override
            public void onResult(ArrayList<DEStyleBean> objects) {
                if (mView != null) {
                    mView.setStyle(objects);
                }
            }
        });
    }


    /**
     * 提交预约
     */
    private Map<String, String> params = new HashMap<>();

    public void reserve(String styleId, String order_area, String contact_name, String contact_mobile,
                        String address_detail, LocationExtra extra) {
        if (StringUtils.isBlank(styleId)) {
            Toaster.showLongToast("请选择类型", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(order_area)) {
            Toaster.showLongToast("请输入面积", MyToast.FAIL);
            return;
        } else if (StringUtils.getDoubleToString(order_area) == 0) {
            Toaster.showLongToast("请输入正确的面积", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(contact_name)) {
            Toaster.showLongToast("请输入联系人", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(contact_mobile)) {
            Toaster.showLongToast("请输入联系人手机号", MyToast.FAIL);
            return;
        } else if (!FieldCheckUtil.checkPhoneNumber(contact_mobile)) {
            Toaster.showLongToast("请输入正确的联系人手机号", MyToast.FAIL);
            return;
        } else if (extra == null) {
            Toaster.showLongToast("请选择地址", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(address_detail)) {
            Toaster.showLongToast("请输入详细地址", MyToast.FAIL);
            return;
        }

        // 获取详细地址
        LocationUtils.reGeocodeSearch(mActivity,
                new LatLonPoint(extra.getLatitude(), extra.getLongitude()),
                new LocationUtils.onReGeocodeSearchedListener() {
                    @Override
                    public void onReGeocodeSearched(RegeocodeAddress result) {
                        params.put("address_provider", result.getProvince());
                        params.put("address_city", result.getCity());
                        params.put("address_country", result.getDistrict());

                        params.put("order_type", mExtra.getType());
                        params.put("style_id", styleId);
                        params.put("order_area", order_area);
                        params.put("contact_name", contact_name);
                        params.put("contact_mobile", contact_mobile);
                        params.put("address_detail", address_detail);

                        mModel.reserve(params, new ObserverOnResultListener<Object>() {
                            @Override
                            public void onResult(Object o) {
                                RouterManager.getInstance().goAndFinish(mActivity, RouterPath.RESERVE_SUCCESS,
                                        new ReserveExtra(mExtra.getType()));
                            }
                        });
                    }
                });
    }


    /**
     * 定制加配下单
     */
    public void order(String contact_name, String contact_mobile,
                      String address_detail, LocationExtra extra, CustomExtra customExtra){
        if (StringUtils.isBlank(contact_name)) {
            Toaster.showLongToast("请输入联系人", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(contact_mobile)) {
            Toaster.showLongToast("请输入联系人手机号", MyToast.FAIL);
            return;
        } else if (!FieldCheckUtil.checkPhoneNumber(contact_mobile)) {
            Toaster.showLongToast("请输入正确的联系人手机号", MyToast.FAIL);
            return;
        } else if (extra == null) {
            Toaster.showLongToast("请选择地址", MyToast.FAIL);
            return;
        } else if (StringUtils.isBlank(address_detail)) {
            Toaster.showLongToast("请输入详细地址", MyToast.FAIL);
            return;
        }

        // 获取详细地址
        LocationUtils.reGeocodeSearch(mActivity,
                new LatLonPoint(extra.getLatitude(), extra.getLongitude()),
                new LocationUtils.onReGeocodeSearchedListener() {
                    @Override
                    public void onReGeocodeSearched(RegeocodeAddress result) {
                        params.put("address_provider", result.getProvince());
                        params.put("address_city", result.getCity());
                        params.put("address_country", result.getDistrict());

                        params.put("customized_id", customExtra.getProjectId());
                        params.put("huxing_id", customExtra.getTypeId());
                        params.put("programme_id", customExtra.getProgramId());
                        params.put("contact_name", contact_name);
                        params.put("contact_mobile", contact_mobile);
                        params.put("address_detail", address_detail);

                        mModel.customOrder(params, new ObserverOnResultListener<Object>() {
                            @Override
                            public void onResult(Object o) {
                                RouterManager.getInstance().goAndFinish(mActivity, RouterPath.RESERVE_SUCCESS,
                                        new ReserveExtra(mExtra.getType()));
                            }
                        });
                    }
                });
    }

}
